package cn.afei.cloud.serivce.impl;
import cn.afei.cloud.entities.Order;
import cn.afei.cloud.feign.CloudCmsFeignService;
import cn.afei.cloud.mapper.OrderMapper;
import cn.afei.cloud.serivce.OrderService;
import cn.afei.cloud.utils.result.ResultVOUtils;
import cn.afei.cloud.utils.result.ResultVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther zzyy
 * @create 2024-01-06 15:40
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private CloudCmsFeignService cloudCmsFeignService;

    @Override
    public ResultVo selectListPage(){
        IPage<Order> orderIPage = orderMapper.selectPage(new Page<Order>(1, 10),null);
        Map<String,Object> mapAll = new HashMap<>();
        mapAll.put("list",orderIPage.getRecords());
        mapAll.put("total",orderIPage.getTotal());
        return ResultVOUtils.success(mapAll);
    }


    @Override
    @GlobalTransactional(name = "zzyy-create-order", rollbackFor = Exception.class) //AT
    public void create(Order order) {
        //xid全局事务id的检查，重要
        String xid = RootContext.getXID();
        //1 新建订单
        log.info("---------------开始新建订单: " + "\t" + "xid: " + xid);
        //订单新建时默认初始订单状态是零
        order.setStatus(0);
        int result = orderMapper.insert(order);
        // 插入订单成功后获得插入mysql的实体对象
        Order orderFromDB = null;

        if (result > 0) {
            // 从mysql里面查出刚插入的记录
            orderFromDB = orderMapper.selectById(order.getId());
            log.info("-----> 新建订单成功,orderFromDB info: " + orderFromDB);
            System.out.println();
            //2 扣减库存
            log.info("-------> 订单微服务开始调用Storage库存，做扣减count");
//            storageFeignApi.decrease(orderFromDB.getProductId(), orderFromDB.getCount());
            log.info("-------> 订单微服务结束调用Storage库存，做扣减完成");
            System.out.println();

            //3 扣减账户余额
            log.info("-------> 订单微服务开始调用Account账号，做扣减money");
            cloudCmsFeignService.decrease(orderFromDB.getUserId(), orderFromDB.getMoney());
            log.info("-------> 订单微服务结束调用Account账号，做扣减完成");
            System.out.println();

            //4 修改订单状态
            //将订单状态从零修改为1，表示已经完成
            log.info("-------> 修改订单状态");
            orderFromDB.setStatus(1);
            Order update = new Order();
            update.setId(order.getId());
            update.setStatus(1);

            int updateResult = orderMapper.updateById(update);
            log.info("-------> 修改订单状态完成" + "\t" + updateResult);
            log.info("-------> orderFromDB info: " + orderFromDB);
        }
        System.out.println();
//        log.info("---------------结束新建订单: " + "\t" + "xid: " + xid);
    }
}
